package com.zzl.finance.data.util;

import com.alibaba.fastjson.JSON;
import com.zzl.finance.data.constant.status.InstitutyStatusEnum;
import com.zzl.general.common.constant.Constant;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.serurity.entity.MenuInfo;
import com.zzl.general.common.utils.KeyValue;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 票据工具类
 *
 * @author : gull
 * @date : 2022/2/18 13:34
 */
public class BaseUtils {

    private static final String GET_STATUS_METHOD_NAME = "getInstitutyStatus";
    private static final String SET_OPERATE_BUTTON_METHOD_NAME = "setOperateButton";
    /**
     * 获取金融机构据对象的 字段值
     *
     * @param ticket 票对象
     * @return 对象值
     */
    public static InstitutyStatusEnum getInstitutyStatus(@NotNull Object ticket) throws SysException {
        Class<?> aClass = ticket.getClass();
        Method method = ReflectionUtils.findMethod(aClass, GET_STATUS_METHOD_NAME);
        if (!ObjectUtils.isEmpty(method)) {
            Object status = ReflectionUtils.invokeMethod(method, ticket);
            if (ObjectUtils.isEmpty(status)) {
                throw new SysException("金融机构状态为空");
            }
            if (status instanceof Integer) {
                return InstitutyStatusEnum.getStatus((Integer) status);
            } else {
                return InstitutyStatusEnum.getStatus(Integer.valueOf(status.toString()));
            }
        } else {
            throw new SysException("未找到金融机构的 getInstitutyStatus 方法");
        }

    }

    /**
     * 设置操作按钮
     *
     * @param ticket    票据对象
     * @param keyValues 操作按钮
     * @throws SysException 业务异常
     */
    public static void setOperateButton(@NotNull Object ticket, List<KeyValue> keyValues) throws SysException {
        Class<?> aClass = ticket.getClass();
        Method method = ReflectionUtils.findMethod(aClass, SET_OPERATE_BUTTON_METHOD_NAME, List.class);
        if (!ObjectUtils.isEmpty(method)) {
            ReflectionUtils.invokeMethod(method, ticket, keyValues);
        } else {
            throw new SysException("未找到票据的 setOperateButton 方法");
        }
    }

    /**
     * 获取按钮权限是否存在
     * true 有权限
     * false 没权限
     *
     * @param session 按钮
     */
    public static boolean getButtonAuth(HttpSession session, String buttonCode) {
        //        session中的按钮权限集合key值：Constant.SESSION_BUTTON
        String buttonList = (String) session.getAttribute(Constant.SESSION_BUTTON);
        List<MenuInfo> menuInfos = JSON.parseArray(buttonList, MenuInfo.class);
        if (!CollectionUtils.isEmpty(menuInfos)) {
            List<String> collect = menuInfos.stream().map(MenuInfo::getMenuAction).distinct().collect(Collectors.toList());
            if (collect.contains(buttonCode)) {
                return true;
            }
        }
        return false;
    }
}
